from jinja2 import Environment, FileSystemLoader


def schemaFilter(schema: dict = None):
    if schema and schema.get('$ref'):
        ref = schema.get('$ref').replace('#/', '')
        schema = f"'tests/fixtures/{ref}.json'"

    return schema


def Template(template_name='base.j2', context: dict = None):
    environment = Environment(loader=FileSystemLoader('templates'))
    environment.filters['schemaFilter'] = schemaFilter

    template = environment.get_template(template_name)
    contents = template.render(**context)
    return contents
